package hongcaosp.app.android.user.settings.wallet;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blackchopper.loginshare.impl.LoginShare;
import com.blackchopper.loginshare.interfaces.OnLoginshareListener;
import com.blackchopper.loginshare.messager.Messager;
import com.blackchopper.loginshare.model.Type;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;

import hongcaosp.app.android.R;
import hongcaosp.app.android.contact.ListActivity;
import hongcaosp.app.android.modle.BaseResponse;
import hongcaosp.app.android.modle.bean.UserWallet;
import hongcaosp.app.android.user.settings.wallet.iview.UserWalletIView;
import hongcaosp.app.android.user.settings.wallet.presenter.WalletPresenter;
import xlj.lib.android.base.component.BaseActivity;
import xlj.lib.android.base.toastcompat.ToastManager;
import xlj.lib.android.base.utils.DialogUtil;

/**
 * 钱包
 */
public class WalletActivity extends BaseActivity implements UserWalletIView,IWXAPIEventHandler,OnLoginshareListener {

    public static void launcher(Context context) {
        context.startActivity(new Intent(context,WalletActivity.class));
    }

    private WalletPresenter presenter;

    private TextView tv_balance,    //余额
            tv_withdraw;    //提现金额
    private TextView tv_chargeMoeny;
    private LinearLayout ll_withdrawWechat;

    private int accountType;
    private String account;
    private LoginShare loginShare;

    @Override
    protected void initData(Intent intent) {
        Messager.getInstance().register(this);
        loginShare = new LoginShare(this);
        loginShare.register(this);

        presenter = new WalletPresenter(this,this);
        presenter.initChargeMoney((RecyclerView) findViewById(R.id.rv_charge_money));
        presenter.loadInfo();
    }

    @Override
    protected void initView() {

        findViewById(R.id.back_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        findViewById(R.id.btn_pay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                presenter.pay();
            }
        });
        ll_withdrawWechat = findViewById(R.id.ll_withdraw_wechat);
        ll_withdrawWechat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //微信提现
                if (presenter.isBindWXAccount()) {
                    toWithdraw(1);
                } else {
                    DialogUtil.DialogBuilder.createBuilder(WalletActivity.this)
                            .setContent("是否确定绑定本机微信？")
                            .setCancelText("取消")
                            .setSureText("确定")
                            .setOnSureListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    toAccountBind(accountType = 1);
                                }
                            })
                            .build()
                            .show();
                }
            }
        });
        findViewById(R.id.ll_withdraw_alipay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //支付宝提现
                if (presenter.isBindALiAccount()) {
                    toWithdraw(0);
                } else {
                    toAccountBind(accountType = 0);
                }
            }
        });
        findViewById(R.id.ll_recharge).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ListActivity.toActivity(WalletActivity.this,ListActivity.LIST_TYPE_RECHARGE);
            }
        });
        findViewById(R.id.ll_withdraw).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ListActivity.toActivity(WalletActivity.this,ListActivity.LIST_TYPE_WITHDRAW);
            }
        });

        tv_balance = findViewById(R.id.tv_balance);
        tv_withdraw = findViewById(R.id.tv_withdraw);
        tv_chargeMoeny = findViewById(R.id.tv_charge_money);
    }

    /**
     * 提现
     * @param type 0-支付宝 1-微信
     */
    private void toWithdraw(int type) {
        String account = type == 0 ? presenter.getUserWallet().getAccountAli() : presenter.getUserWallet().getAccountWx();
        WithdrawActivity.launcher(WalletActivity.this,type,account,presenter.getReadAmount(),88);
    }

    /**
     * 绑定提现账号
     * @param type 0-支付宝 1-微信
     */
    private void toAccountBind(int type) {
        if (type == 0) {
            //支付宝授权
            ToastManager.getInstance().showToast("请绑定支付宝账号");
        } else {
            //微信授权
            ToastManager.getInstance().showToast("请绑定微信账号");
            loginShare.launchWechatLogin();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK ) {
            if (requestCode == 88) {
                presenter.loadInfo();
            }
        }
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_wallet;
    }

    @Override
    public void walletInfo(UserWallet userWallet) {
        if (userWallet != null) {
            tv_balance.setText("" + userWallet.getAmount());
            tv_withdraw.setText("" + userWallet.getRedAmount());
        }
    }

    @Override
    public void selectMoneyItem(Double price) {
        tv_chargeMoeny.setText("支付金额：" + price + "元");
    }

    @Override
    public void cashWithdrawalRst(BaseResponse response) {
        //绑定提现账号
        ToastManager.getInstance().showToast("绑定成功");

        if (presenter.getUserWallet() != null) {
            presenter.getUserWallet().setBindWx(true);
            presenter.getUserWallet().setAccountWx(account);
        }
        WithdrawActivity.launcher(this,1,account,presenter.getReadAmount(),88);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Messager.getInstance().unRegister(this);
        loginShare.unRegister();
    }

    //微信支付回调
    @Override
    public void onReq(BaseReq baseReq) {
        
    }

    @Override
    public void onResp(BaseResp baseResp) {
        if (baseResp == null) {
            ToastManager.getInstance().showToast("未知错误");
            return;
        }
        if (baseResp.getType() == 5) {
            //支付
            if (baseResp.errCode == 0) {//支付成功
                ToastManager.getInstance().showToast("支付成功");
                presenter.loadInfo();
            } else if (baseResp.errCode == -2) {//用户取消支付
                ToastManager.getInstance().showToast("支付已取消");
            } else { //支付失败
                ToastManager.getInstance().showToast("支付失败");
            }
        }
    }


    //微信授权=================================
    @Override
    public void onLoginSuccess(Type type, String code, String info) {
        presenter.bindingPayType(accountType,account = info);
    }

    @Override
    public void onLoginCancel(Type type) {
        ToastManager.getInstance().showToast("授权取消");
    }

    @Override
    public void onLoginError(Type type, int errorCode) {
        ToastManager.getInstance().showToast("授权失败");
    }

    @Override
    public void onShareSuccess(Type type) {

    }

    @Override
    public void onShareCancel(Type type) {

    }

    @Override
    public void onShareError(Type type, int code) {

    }
}
